<template>
  <div class="dashboard">
    <el-row type="flex" :gutter="15" justify="space-around">
      <el-col :span="6">
        <div class="info-box box-1">
          <img src="@/assets/img/admin-icon.png" alt="" />
          <p>
            管理员用户 <br />
            <b>{{admin_num}}</b> 名
          </p>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="info-box box-2">
          <img src="@/assets/img/user-icon.png" alt="" />
          <p>
            平台注册用户 <br />
            <b>{{user.total}}</b>名
          </p>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="info-box box-3">
          <img src="@/assets/img/article-icon.png" alt="" />
          <p>
            平台文章总数 <br />
            <b>{{article_num}}</b>篇
          </p>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="info-box box-4">
          <img src="@/assets/img/adv-icon.png" alt="" />
          <p>
            广告位 <b>{{advpos_num}}</b>个 <br />
            广告图 <b>{{advimg_num}}</b> 张
          </p>
        </div>
      </el-col>
    </el-row>
    <el-row type="flex" :gutter="15">
      <el-col :span="8">
        <div class="tab-container">
          <div class="title"><span>最新注册新用户</span></div>
          <div class="box">
            <div class="userlist el-row" v-for="item in new_user" :key="item.id">
              <div class="info el-col el-col-7">
                &nbsp;&nbsp; {{item.username}}
              </div>
              <div class="from el-col el-col-4">{{ item.sex*1 ===0 ? '男':'女' }}</div>
              <div class="nums el-col el-col-13">{{item.address}}</div>
            </div>
          </div>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="tab-container">
          <div class="title"><span>点赞文章排行</span></div>
          <div class="box">
            <div class="article el-row">
              <div class="info"  v-for="item in hot_article" :key="item.id">
                {{item.title}}
              </div>
            </div>
          </div>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="tab-container">
          <div class="title"><span>用户性别分析</span></div>
          <div style="width:408px;height:280px;" id="userdata"> </div>
          </div>
      </el-col>
    </el-row>
    <div id="cate-data">

    </div>

  </div>
</template>
<script>
import { getBaseData } from '@/api/index'
import * as echarts from 'echarts'
import sexData from './sexData'
import articleData from './articleData'
export default {
  name: 'Dashboard',
  components: {},
  props: {},
  data () {
    return {
      admin_num: 0,
      advimg_num: 0,
      advpos_num: 0,
      all_cate: [],
      article_num: 0,
      cate_num: 6,
      hot_article: [],
      new_user: [],
      tag_num: 0,
      user: {}
    }
  },
  created () {
    this.initData()
  },
  mounted () {
    this.myChart = echarts.init(document.getElementById('userdata'))
    this.myChart2 = echarts.init(document.getElementById('cate-data'))
    this.myChart.setOption(sexData)
    this.myChart2.setOption(articleData)
  },
  methods: {
    async initData () {
      const res = await getBaseData()
      console.log(res)
      this.admin_num = res.admin_num
      this.advimg_num = res.advimg_num
      this.advpos_num = res.advpos_num
      this.all_cate = res.all_cate
      this.article_num = res.article_num
      this.cate_num = res.cate_num
      this.hot_article = res.hot_article
      this.new_user = res.new_user
      this.tag_num = res.tag_num
      this.user = res.user
      // 性别数据
      sexData.series[0].data = [
        { value: this.user.user_woman, name: '女性' },
        { value: this.user.user_man, name: '男性' }
      ]
      this.myChart.setOption(sexData)
      // 分类数量
      const xData = this.all_cate.map(val => val.catename)
      const yData = this.all_cate.map(val => val.num)
      articleData.xAxis.data = xData
      articleData.series[0].data = yData
      this.myChart2.setOption(articleData)
    }
  }
}
</script>
<style lang="scss" scoped>
.dashboard {
  padding: 0 10px;
}
.info-box {
  height: 90px;
  padding-top: 15px;
  border-radius: 5px;
  color: #fff;
  position: relative;
  -webkit-transition: all 0.3s linear;
  transition: all 0.3s linear;
  top: 0;
  overflow: hidden;
  img {
    height: 40px;
    margin-left: 40px;
    margin-right: 20px;
    position: relative;
    top: 2px;
  }
  p {
    display: inline-block;
    padding-top: 10px;
  }
}
.box-1 {
  background: #fb6d49;
  margin-left: 0;
}
.box-2 {
  background: #67c23a;
}
.box-3 {
  background: #e6a23c;
}
.box-4 {
  background: #3a7ce7;
  margin-right: 0;
}

.tab-container {
  background-color: #fff;
  border: 1px solid #ccc;
  border-radius: 5px;
  margin-top: 20px;
  overflow: hidden;
  position: relative;
  -webkit-transition: all 0.3s linear;
  transition: all 0.3s linear;
  top: 0;
}
.tab-container:hover {
  -webkit-box-shadow: 3px 3px 10px #dce3e6;
  box-shadow: 3px 3px 10px #dce3e6;
  top: 2px;
}
.tab-container .title {
  background-color: #ddd;
  line-height: 44px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  padding: 0 15px;
}
.tab-container .box {
  height: 280px;
}
.tab-container .userlist {
  line-height: 40px;
  border-bottom: 1px dashed #eee;
  font-size: 14px;
}
.tab-container .userlist:last-child {
  border: none;
}
.tab-container .userlist img {
  vertical-align: middle;
  position: relative;
  top: -2px;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  margin-left: 10px;
}
.article{
  .info{
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 41px;
    border-bottom: 1px dashed #eee;
    font-size: 14px;
    padding: 0 10px;
  }
}
.mr20 {
  margin-right: 20px;
}
#cate-data{
  height: 400px;
  background-color: #fff;
  margin-top: 20px;
  border-radius: 5px;
}
</style>
